import { http } from "@/utils/http";

// 库存流水项目接口
export interface InventoryFlowItem {
  id: number;
  number: string; // 编号
  status: number; // 状态
  person: string; // 操作人
  vendorId: number; // 供应商ID
  repositoryId: number; // 仓库ID
  remark?: string; // 备注
  createTime?: string; // 创建时间
  updateTime?: string; // 更新时间
  tenantId?: number; // 租户ID
  materialPriceList?: Array<{
    materialId: number;
    price: number;
    quantity: number;
  }>; // 物料价格列表
}

// 查询库存流水列表参数
export interface InventoryFlowListParams {
  page?: number;
  limit?: number;
  query?: string;
  tenantId?: number;
}

// 查询库存流水列表返回结果
export interface InventoryFlowListResult {
  code: number;
  data: object;
  message: string;
}

// 添加/修改库存流水参数
export interface InventoryFlowPayload {
  number?: string;
  status?: number;
  person?: string;
  vendorId?: number;
  repositoryId?: number;
  remark?: string;
  materialPriceList?: Array<{
    materialId: number;
    price: number;
    quantity: number;
  }>;
}

// 创建库存流水返回结果
export interface InventoryFlowCreateResult {
  code: number;
  data: object;
  message: string;
}

// 更新库存流水返回结果
export interface InventoryFlowUpdateResult {
  code: number;
  data: object;
  message: string;
}

// 根据ID查询库存流水返回结果
export interface InventoryFlowDetailResult {
  code: number;
  data: object;
  message: string;
}

// 批量删除库存流水返回结果
export interface InventoryFlowBatchDeleteResult {
  code: number;
  data: object;
  message: string;
}

// 查询所有库存流水返回结果
export interface InventoryFlowAllListResult {
  code: number;
  data: object;
  message: string;
}

// 删除单个库存流水返回结果
export interface InventoryFlowDeleteResult {
  code: number;
  data: object;
  message: string;
}

/**
 * 查询库存流水列表
 * @param data 查询参数
 * @returns 库存流水列表数据
 */
export const getInventoryFlowList = (data: InventoryFlowListParams) => {
  return http.request<InventoryFlowListResult>("post", "inventoryFlow/list", { data });
};

/**
 * 添加库存流水信息
 * @param data 库存流水信息
 * @returns 创建结果
 */
export const createInventoryFlow = (data: InventoryFlowPayload) => {
  return http.request<InventoryFlowCreateResult>("post", "inventoryFlow/add", { data });
};

/**
 * 修改库存流水信息
 * @param data 库存流水信息（包含id）
 * @returns 修改结果
 */
export const updateInventoryFlow = (data: InventoryFlowPayload & { id: number }) => {
  return http.request<InventoryFlowUpdateResult>("post", "inventoryFlow/update", { data });
};

/**
 * 根据ID查询库存流水信息
 * @param id 库存流水ID
 * @returns 库存流水详细信息
 */
export const getInventoryFlowById = (id: number) => {
  return http.request<InventoryFlowDetailResult>("get", "inventoryFlow/find", { params: { id } });
};

/**
 * 批量删除库存流水信息
 * @param data 包含id数组的请求体
 * @returns 删除结果
 */
export const deleteBatchInventoryFlow = (data: { ids: number[] }) => {
  return http.request<InventoryFlowBatchDeleteResult>("post", "inventoryFlow/deleteBatch", { data });
};

/**
 * 查询所有库存流水信息
 * @returns 所有库存流水信息
 */
export const getAllInventoryFlow = () => {
  return http.request<InventoryFlowAllListResult>("get", "inventoryFlow/findAll");
};

/**
 * 删除单个库存流水信息
 * @param id 库存流水ID
 * @returns 删除结果
 */
export const deleteInventoryFlow = (id: number) => {
  return http.request<InventoryFlowDeleteResult>("get", "inventoryFlow/delete", { params: { id } });
};

// 库存材料项接口
export interface InventoryMaterialItem {
  id: number;
  materialId: number;
  materialName: string;
  brand: string;
  specification: string;
  unit: string;
  stocks: number; // 库存数量
  outboundPrice: number; // 建议零售价
  remark?: string;
}

// 查询库存材料列表参数
export interface InventoryMaterialListParams {
  page?: number;
  limit?: number;
  repositoryId: number;
  name?: string;
  brand?: string;
}

// 查询库存材料列表返回结果
export interface InventoryMaterialListResult {
  code: number;
  data: {
    records?: InventoryMaterialItem[];
    list?: InventoryMaterialItem[];
    total?: number;
  };
  message: string;
}

/**
 * 获取指定仓库的库存材料信息列表
 * @param data 查询参数
 * @returns 库存材料列表
 */
export const getInventoryMaterialList = (data: InventoryMaterialListParams) => {
  return http.request<InventoryMaterialListResult>("post", "inventoryRefinement/list", { data });
};